package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.io.Serializable;
import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.picture.IPictureView;
import liuliu.throughwaring.control.picture.PictureListener;
import liuliu.throughwaring.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;

/**
 * Created by liuliu on 2015/11/18   18:24
 * 单位详细信息
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class UnitDetailActivity extends BaseActivity implements IPictureView {
    @CodeNote(id = R.id.unit_detail_name)
    TextView unit_detail_name;//单位名称
    @CodeNote(id = R.id.unit_detail_type)
    TextView unit_detail_type;//单位类型
    @CodeNote(id = R.id.unit_detail_phone)
    TextView unit_detail_phone;//联系电话
    @CodeNote(id = R.id.unit_detail_Legal)
    TextView unit_detail_Legal;//法人
    @CodeNote(id = R.id.unit_detail_Security)
    TextView unit_detail_Security;//治安
    @CodeNote(id = R.id.unit_detail_address)
    TextView unit_detail_address;//单位地址
    @CodeNote(id = R.id.unit_detail_cratetime)
    TextView unit_detail_cratetime;//创建时间
    @CodeNote(id = R.id.unit_detail_remark)
    TextView unit_detail_remark;//备注
    @CodeNote(id = R.id.unit_detail_tel)
    Button unit_detail_tel;//拨打电话
    CompanyModel companyModel;
    @CodeNote(id = R.id.unit_detail_image)
    ImageView unit_detail_image;
    @CodeNote(id = R.id.unit_detail_toolbar)
    TToolbar unit_detail_toolbar;
    @CodeNote(id = R.id.unit_detail_write)
    Button writeBtn;
    @CodeNote(id = R.id.unit_detail_changetime)
    TextView changetiem;
    List<CodeModel> mCodeList;
    PictureListener listener;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_unit_detail);
        mCodeList = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_CompanyType'");
        listener = new PictureListener(this, this);

        Intent intent = getIntent();
        companyModel = new CompanyModel();
        companyModel = (CompanyModel) intent.getSerializableExtra("CompanyModel");

        setModel();
        listener.getPicture(companyModel.getCompanyOveralImageId());
    }

    private String getCodeyCompanyValue(String value) {
        for (int i = 0; i < mCodeList.size(); i++) {
            if (value.equals(mCodeList.get(i).getName())) {
                return mCodeList.get(i).getValue();//流动人员
            }
        }
        return "";
    }

    private void setModel() {
        unit_detail_name.setText(companyModel.getCompanyName());
        unit_detail_remark.setText(companyModel.getCompanyComment());
        unit_detail_type.setText(getCodeyCompanyValue(companyModel.getCompanyType()).substring(0, 4));
        unit_detail_phone.setText(companyModel.getCompanyPhoneNumber());
        unit_detail_Legal.setText(companyModel.getCompanyLegalPerson());
        unit_detail_Security.setText(companyModel.getCompanySecurityPerson());
        unit_detail_address.setText(companyModel.getCompanyAddress());
        unit_detail_cratetime.setText(companyModel.getCompanyCreateTime());
        changetiem.setText(companyModel.getCompanyChangeTime());
        if (!companyModel.getCompanyOveralImage().equals("null")) {
            unit_detail_image.setImageBitmap(Utils.getBitmapByte(companyModel.getCompanyOveralImage()));

        }
    }

    @Override
    public void initEvents() {
        unit_detail_toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
        unit_detail_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //用intent启动拨打电话
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + companyModel.getCompanyPhoneNumber()));
                startActivity(intent);
            }
        });
        writeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UnitDetailActivity.this, AddWorkActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("CompanyModel", (Serializable) companyModel);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void PictureResult(boolean isResult, String pbyte) {
        if (isResult) {
            Bitmap bitmap = Utils.getBitmapByte(pbyte);
            companyModel.setCompanyOveralImage(pbyte);
            finalDb.update(companyModel);
            unit_detail_image.setImageBitmap(bitmap);
        }
    }
}
